<?php

namespace Mobile\Controller;

use Think\Controller;

/**
* APP下载中转
* 
*/ 
class AppController extends Controller{

	protected $user = NULL;

	public function __construct()
	{
		parent::__construct();
		$this->user = session('user');
	}

	// APP注册
	public function reg()
	{
		if( IS_GET )
		{
			if( $this->user )
			{
				return $this->index();
			}

			$shareParam = session('shareParam');

			$this->assign("incode", isset($shareParam['incode']) ? $shareParam['incode'] : '' );
			$this->assign("uuid", isset($shareParam['uuid']) ? $shareParam['uuid'] : '' );
			$this->assign("sence", isset($shareParam['sence']) ? $shareParam['sence'] : '' );
			return $this->display('reg');
		}
		
		$username = I('post.username','');
        $password = I('post.password','');
		$repassword = I('post.repassword','');
		
		if( !empty($password) )
		{
			$password = md5($password);
		}

		if( !empty($repassword) )
		{
			$repassword = md5($repassword);
		}

        $first_leader = I('post.first_leader','');//邀请码
		
		$this->userLogic = new \Home\Logic\UsersLogic();
        $unique_id = I('unique_id');
        //是否开启注册验证码机制
        if(check_mobile($username) && TpCache('sms.regis_sms_enable')){
            $code = I('post.code');
			if(empty($code))
				return $this->returnInfo(-1,'请输入验证码');
            $check_code = $this->userLogic->sms_code_verify($username,$code,$unique_id);
			if($check_code['status'] != 1)
				return $this->returnInfo(-1,$check_code['msg'] ? $check_code['msg']:'验证失败');
        }
        $data = $this->userLogic->reg($username,$password , $repassword, $first_leader);
		
		if($data && (int)$data['status'] === 1)
		{
			session('user',$data['result'],86400);
		}
		
		return $this->returnInfo($data['status'],$data['msg'],$data['result']);
	}

	// 分享落地页&&APP下载 
	public function index()
	{
		$InCode = I("incode","");//邀请码
		$InCodeUid = I("uuid","");//邀请码user_id
		$sence = I("sence","");//分享的场景，备用
		$dl = I("dl",0);//是否强制下载

		if( $InCode || $InCodeUid || $sence  ) session("shareParam",[
			"incode" => $InCode,//邀请码
			"uuid" => $InCodeUid,//邀请码user_id
			"sence" => $sence,//分享的场景，备用
		],86400);

		if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')
			||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')
			||strpos($_SERVER['HTTP_USER_AGENT'], 'Mac')
			)
		{
		    $url = $this->getDownLoadUrl(1);
		}else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
		    $url = $this->getDownLoadUrl(2);
		}else{
		    $url = $this->getDownLoadUrl(2);
		}

		$is_weixin = is_weixin();
		$this->assign("is_weixin",$is_weixin);
		$this->assign("download",$url);

		if( $dl > 0 && !$is_weixin )
		{
			return header("location:".$url);
		}

		if( !$this->user &&  $InCode )
		{
			return $this->reg();
		}
		
		if( $is_weixin|| empty($url)  )
		{
			return $this->display('index');
		}

		header("location:".$url);
	}

	protected function getDownLoadUrl($platform = 1)
	{
		$res = M("app_version")->where([
				"platform" => $platform,
				"status" => 1,
			])->order("app_ver_num desc,app_ver_code")->cache(120)->field("download_url")->find();
		return $res ? parseMediaUrl( $res['download_url'] ) : "";

	}


	protected function returnInfo($code = 0,$msg = "",$data = '')
	{ 
		header('Content-Type:application/json; charset=utf-8');
		echo json_encode([
			"status" => $code,
			"msg" => $msg,
			"result" => $data
		],JSON_UNESCAPED_UNICODE);
	}

}